package com.coder4j.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.coder4j.entity.SDisplayInterfaceEntity;
import com.coder4j.entity.STaskFlowEntity;
import com.coder4j.mapper.SDisplayInterfaceMapper;
import com.coder4j.utils.ListUtils;
import com.coder4j.vo.MenuData;
import com.coder4j.vo.SDisplayInterfaceQuery;
import com.coder4j.vo.SDisplayInterfaceVO;
import com.coder4j.starter.web.BaseServiceImpl;
import com.coder4j.vo.STaskFlowQuery;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 显示界面
 * @author keller
 * @date 2025-03-18 16:22:25
 */
@Service
@Slf4j
public class SDisplayInterfaceService extends BaseServiceImpl<SDisplayInterfaceMapper,SDisplayInterfaceEntity,SDisplayInterfaceVO> {

    @Resource
    private SDisplayInterfaceMapper mapper;

    @Resource
    private SDisplayInterfaceDetailService sDisplayInterfaceDetailService;

    @Override
    public SDisplayInterfaceVO entity2Vo(SDisplayInterfaceEntity entity) {
        SDisplayInterfaceVO vo = new SDisplayInterfaceVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public SDisplayInterfaceEntity vo2Entity(SDisplayInterfaceVO vo) {
        SDisplayInterfaceEntity entity = new SDisplayInterfaceEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<MenuData> getMenuData(){
        List<MenuData> list = new ArrayList<>();
        SDisplayInterfaceQuery query = new SDisplayInterfaceQuery();
        List<SDisplayInterfaceEntity> entityList = this.baseQueryList(query);
        if(ListUtils.isEmpty(entityList)){
            return list;
        }

        Map<String, List<SDisplayInterfaceEntity>> map = entityList.stream().collect(Collectors.groupingBy(SDisplayInterfaceEntity::getDisplayType));
        for (String type : map.keySet()) {
            MenuData data = new MenuData();
            data.setKey(type);
            data.setLabel(type);
            data.setChildren(new ArrayList<>());
            for (SDisplayInterfaceEntity entity : map.get(type)) {
                MenuData child = new MenuData();
                child.setKey(entity.getId()+"");
                child.setLabel(entity.getDisplayName());
                data.getChildren().add(child);
            }
            list.add(data);
        }
        return list;
    }

    public void add(JSONObject jsonObject) {
        //获取ID
        String id = jsonObject.getStr("id");
        //图片地址
        String imageUrl = jsonObject.getStr("imageUrl");
        //评分输入
        JSONObject scores = jsonObject.getJSONObject("scores");
        //闪烁
        String flicker = scores.getStr("flicker");
        //显示字符
        String displayChar = scores.getStr("displayChar");
        //颜色匹配
        String colorMatch = scores.getStr("colorMatch");
        //布局位置
        String layoutPosition = scores.getStr("layoutPosition");
        SDisplayInterfaceVO sDisplayInterfaceVO = this.baseGetVOById(id);
        sDisplayInterfaceVO.setImg(imageUrl);
        sDisplayInterfaceVO.setFlashing(flicker);
        sDisplayInterfaceVO.setCharacters(displayChar);
        sDisplayInterfaceVO.setColorMatching(colorMatch);
        sDisplayInterfaceVO.setLayoutPosition(layoutPosition);
        sDisplayInterfaceVO.setTaskFlowId(jsonObject.getInt("taskFlowId"));
        this.baseSaveVO(sDisplayInterfaceVO);
        sDisplayInterfaceDetailService.updata(id,jsonObject);
    }

    public JSONObject getOne(Integer id) {
        JSONObject respJson = new JSONObject();
        SDisplayInterfaceVO sDisplayInterfaceVO = this.baseGetVOById(id);

        JSONObject scores = new JSONObject();
        scores.putOnce("flicker",sDisplayInterfaceVO.getFlashing());
        scores.putOnce("displayChar",sDisplayInterfaceVO.getCharacters());
        scores.putOnce("colorMatch",sDisplayInterfaceVO.getColorMatching());
        scores.putOnce("layoutPosition",sDisplayInterfaceVO.getLayoutPosition());
        respJson.putOnce("scores",scores);
        JSONArray detail = sDisplayInterfaceDetailService.getOne(id);
        respJson.putOnce("regions",detail);
        respJson.putOnce("id",String.valueOf(id));
        respJson.putOnce("imageUrl",sDisplayInterfaceVO.getImg());
        respJson.putOnce("taskFlowId",sDisplayInterfaceVO.getTaskFlowId());
        return respJson;
    }

    public Object opList() {
        SDisplayInterfaceQuery sDisplayInterfaceQuery = new SDisplayInterfaceQuery();
        List<SDisplayInterfaceEntity> sDisplayInterfaceEntities = this.baseQueryList(sDisplayInterfaceQuery);
        List<JSONObject> respList = new ArrayList<>();
        for (SDisplayInterfaceEntity sDisplayInterfaceEntity : sDisplayInterfaceEntities) {
            JSONObject entries = new JSONObject();
            entries.putOnce("value",sDisplayInterfaceEntity.getId());
            entries.putOnce("label",sDisplayInterfaceEntity.getDisplayName());
            respList.add(entries);
        }
        return respList;
    }
}
